/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | 单位工程信息详情模板渲染
 * +----------------------------------------------------------------------
 */
layui.define(['bluebird', 'templateInit', 'jquery', 'element', 'configureCom', 'templates', 'selectFun', 'matchDict', 'jsDictFile', 'configure'], function (exports) {
    var templateInit = layui.templateInit;
    var $ = layui.$;
    var templateHtml = $('#templateId').html();
    var element = layui.element;
    var configureCom = layui.configureCom;
    var templates = layui.templates;
    var selectFun = layui.selectFun;
    var matchDict = layui.matchDict;
    var jsDictFile = layui.jsDictFile;
    var configure = layui.configure;
    var bdstType = configureCom.GetQueryString('bdstType');
    var singleProjectId = configureCom.GetQueryString('singleProjectId');
    window.dwShow = {//要显示的单位
        'kc': false,
        'sj': false
    };
    if (bdstType) {
        if (bdstType === '1010') {
            dwShow.kc = true;
        } else if (bdstType === '1020') {
            dwShow.sj = true;
        }
    }
    window.gcName = bdstType === '1010' ? '勘察' : '设计';

    var singleProjectInfoDetail = {
        data: {
            projectInfo: {},
            singleProjectInfo: {
                projectMajorType: {}
            },
            surveyUnitInfo: {},
            designUnitInfo: {},
            filesInfo: {}//附件信息
        },
        render: function (url) {
            templates.param.searchData.singleProjectId = singleProjectId;
            templates.getSingleProjectInfoDetail(url).then(function (res) {

                var kcSjDto = res.result.kcSjdwMsgDtos;
                var kcId = '';
                var sjId = '';
                $.each(kcSjDto, function (index, item) {
                    if (item.projBdstType == '1010') {
                        kcId = item.projBdstId;
                        dwShow.kc = true;
                    } else if (item.projBdstType == '1020') {
                        sjId = item.projBdstId;
                        dwShow.sj = true;
                    }
                });
                singleProjectInfoDetail.data.filesInfo.kcCjFjUrl = configureCom.nocache(configure.ATTA_LIST_HTML + '?checkType=23&type=detail&dataId=' + kcId);
                singleProjectInfoDetail.data.filesInfo.kcDjFjUrl = configureCom.nocache(configure.ATTA_LIST_HTML + '?checkType=26&type=detail&dataId=' + singleProjectId || '');
                singleProjectInfoDetail.data.filesInfo.sjCjFjUrl = configureCom.nocache(configure.ATTA_LIST_HTML + '?checkType=33&type=detail&dataId=' + sjId);
                singleProjectInfoDetail.data.filesInfo.sjDjFjUrl = configureCom.nocache(configure.ATTA_LIST_HTML + '?checkType=30&type=detail&dataId=' + singleProjectId || '');

                singleProjectInfoDetail.data.projectInfo = res.result.mainProjDetail;
                var projType = matchDict.data(singleProjectInfoDetail.data.projectInfo.projType, jsDictFile.projType);
                var useType = null;
                if (singleProjectInfoDetail.data.projectInfo.useType && singleProjectInfoDetail.data.projectInfo.useType !== '') {
                    if (projType) {
                        useType = matchDict.data(singleProjectInfoDetail.data.projectInfo.useType, projType.childrens);
                        if (useType) {
                            singleProjectInfoDetail.data.projectInfo.useType = useType.name;
                        }
                    }
                }
                if (singleProjectInfoDetail.data.projectInfo.projType && singleProjectInfoDetail.data.projectInfo.projType !== '') {
                    if (projType) {
                        singleProjectInfoDetail.data.projectInfo.projType = projType.name;
                    }
                }
                if (singleProjectInfoDetail.data.projectInfo.buildScale && singleProjectInfoDetail.data.projectInfo.buildScale !== '') {
                    singleProjectInfoDetail.data.projectInfo.buildScale = matchDict.data(singleProjectInfoDetail.data.projectInfo.buildScale, jsDictFile.buildScale).name;
                }
                if (singleProjectInfoDetail.data.projectInfo.buildType && singleProjectInfoDetail.data.projectInfo.buildType !== '') {
                    singleProjectInfoDetail.data.projectInfo.buildType = matchDict.data(singleProjectInfoDetail.data.projectInfo.buildType, jsDictFile.buildType).name;
                }
                if (singleProjectInfoDetail.data.projectInfo.buildMode && singleProjectInfoDetail.data.projectInfo.buildMode !== '') {
                    singleProjectInfoDetail.data.projectInfo.buildMode = matchDict.data(singleProjectInfoDetail.data.projectInfo.buildMode, jsDictFile.buildMode).name;
                }
                if (singleProjectInfoDetail.data.projectInfo.country && singleProjectInfoDetail.data.projectInfo.country !== '') {
                    singleProjectInfoDetail.data.projectInfo.country = selectFun.getAreaByCode(singleProjectInfoDetail.data.projectInfo.country);
                }
                else if (singleProjectInfoDetail.data.projectInfo.city && singleProjectInfoDetail.data.projectInfo.city !== '') {
                    singleProjectInfoDetail.data.projectInfo.country = selectFun.getAreaByCode(singleProjectInfoDetail.data.projectInfo.city);
                }
                if (singleProjectInfoDetail.data.projectInfo.planStartDate && singleProjectInfoDetail.data.projectInfo.planStartDate !== '') {
                    singleProjectInfoDetail.data.projectInfo.planStartDate = configureCom.dateToStr(singleProjectInfoDetail.data.projectInfo.planStartDate);
                }
                if (singleProjectInfoDetail.data.projectInfo.planEndDate && singleProjectInfoDetail.data.projectInfo.planEndDate !== '') {
                    singleProjectInfoDetail.data.projectInfo.planEndDate = configureCom.dateToStr(singleProjectInfoDetail.data.projectInfo.planEndDate);
                }
                singleProjectInfoDetail.data.singleProjectInfo.singleProjectName = res.result.singleProjName || '';
                singleProjectInfoDetail.data.singleProjectInfo.auditNumber = res.result.singleProjCode || '';
                singleProjectInfoDetail.data.singleProjectInfo.devProjectName = res.result.mainProjDetail.mainProjName || '';
                singleProjectInfoDetail.data.singleProjectInfo.devUnitName = res.result.jianSheOrgDetail.orgName || '';
                singleProjectInfoDetail.data.singleProjectInfo.devUnitManager = res.result.mainProjDetail.people || '';
                singleProjectInfoDetail.data.singleProjectInfo.devUnitManagerPhone = res.result.mainProjDetail.mobile || '';
                singleProjectInfoDetail.data.singleProjectInfo.devUnitManagerMail = res.result.mainProjDetail.email || '';
                singleProjectInfoDetail.data.singleProjectInfo.money = res.result.mainProjDetail.projAmt || '';
                singleProjectInfoDetail.data.singleProjectInfo.singleProjectDivision = selectFun.getAreaByCode(res.result.country);
                singleProjectInfoDetail.data.singleProjectInfo.singleAddress = res.result.address || '';
                singleProjectInfoDetail.data.singleProjectInfo.latitude = (res.result.longtitude ? '经度:' + res.result.longtitude : '') + (res.result.latitude ? ';纬度:' + res.result.latitude : '');
                singleProjectInfoDetail.data.singleProjectInfo.singleProjectContent = res.result.description || '';
                singleProjectInfoDetail.data.singleProjectInfo.surveyGrade = configureCom.getJsName('kcdjType', res.result.surveyGrade);
                if (res.result.mainProjDetail) {
                    singleProjectInfoDetail.data.singleProjectInfo.projectAddress = res.result.mainProjDetail.address || '';
                }
                else {
                    singleProjectInfoDetail.data.singleProjectInfo.projectAddress = '';
                }
                if (res.result.kanChaOrgDetail) {
                    singleProjectInfoDetail.data.surveyUnitInfo.surveyProjectName = res.result.kanChaOrgDetail.orgName || '';
                }
                else {
                    singleProjectInfoDetail.data.surveyUnitInfo.surveyProjectName = '';
                }
                if (res.result.shejiOrgDetail) {
                    singleProjectInfoDetail.data.designUnitInfo.designProjectName = res.result.shejiOrgDetail.orgName || '';
                }
                else {
                    singleProjectInfoDetail.data.designUnitInfo.designProjectName = '';
                }
                singleProjectInfoDetail.data.surveyUnitInfo.surveyReportNumber = res.result.kcReportProjCode || '';
                singleProjectInfoDetail.data.surveyUnitInfo.surveyUnitCredentials = '';
                singleProjectInfoDetail.data.designUnitInfo.designReportNumber = res.result.sjReportProjCode || '';
                singleProjectInfoDetail.data.designUnitInfo.designUnitCredentials = '';
                singleProjectInfoDetail.data.dwRes = res.result.dwRes;
                var typeArray = [];
                var createType = true;//是否新增类型,默认新增
                var updateItem = null;//需要更新的类型
                $(res.result.specialTypeItemValueDTOList).each(function (index, item) {
                    if (item.specialItemValueShow && item.specialItemValueShow !== '') {
                        createType = true;
                        updateItem = null;
                        var value = (item.inputType === 'Text' ? item.specialItemValue : item.specialItemValueShow);
                        $(typeArray).each(function (index1, item1) {
                            //判断类别是否已经存在，已存在则不添加
                            if (item1.typeId === item.type) {
                                createType = false;
                                updateItem = item1;
                                return true;
                            }
                        });
                        if (createType) {
                            var typeId = item.type;
                            var typeName = item.chineseType;
                            var desc = item.specialItemName + ':' + (value || '') + (item.unitName || '') + ';';
                            typeArray.push({
                                typeId: typeId,//类别ID
                                typeName: typeName,
                                desc: desc
                            });
                        } else {
                            updateItem.desc = updateItem.desc + item.specialItemName + ':' + (value || '') + (item.unitName || '') + ';';
                        }
                    }
                });
                singleProjectInfoDetail.data.singleProjectInfo.projectMajorType.typeName = configureCom.getSpecialTypeDesc(res.result.specialType);
                singleProjectInfoDetail.data.singleProjectInfo.projectMajorType.majors = typeArray;
                templateInit.init('view', singleProjectInfoDetail.data, templateHtml);
                element.render('collapse');

                try {
                    parent.dtLoaded();
                } catch (e) {

                }
            });
        }
    };
    exports('singleProjectInfoDetail', singleProjectInfoDetail);
});
